﻿using System;
using System.Configuration;
using System.ServiceProcess;
using System.Timers;

namespace HostsUpdaterCore {
    partial class HostsUpdaterService : ServiceBase {
        private Timer _timer;
        private static double _interval = Double.Parse(ConfigurationManager.AppSettings["Interval"]);
        private string _path = ConfigurationManager.AppSettings["Path"];


        public HostsUpdaterService() {
            InitializeComponent();
        }

        protected override void OnStart(string[] args) {
            _timer = new Timer();
            _timer.Elapsed += new ElapsedEventHandler(Process);
            _timer.Enabled = true;
            _timer.Start();
        }

        protected override void OnStop() {
            _timer.Elapsed -= new ElapsedEventHandler(Process);
            _timer.Stop();
        }

        private void Process(object sender, ElapsedEventArgs e) {
            Just_Ping just_Ping = new Just_Ping("google.com");
            just_Ping.Query();
            just_Ping.GenerateHostsFile(_path);
        }
    }
}
